package com.dazhao.animejaphome.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.dazhao.animejaphome.R;
import com.dazhao.animejaphome.bean.ResTextBean;
import com.dazhao.animejaphome.widget.DzVideo;
import com.google.gson.Gson;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;

import cn.jzvd.JZVideoPlayerStandard;

/**
 * Created by zhao on 2017/10/3.
 */

public class AnimeResAdapter extends RecyclerView.Adapter{
    private ArrayList<String> mData;
    private Context context;
    private ArrayList<String> deleteDatas=new ArrayList<>();
    private ArrayList<CheckBox> checkBoxs=new ArrayList<>();
    private View.OnClickListener jt;



    public AnimeResAdapter(ArrayList<String> mData, Context context){
        this.mData=mData;//只是取得了资源名的列表，json并没有取出标题和文本，文本也没有翻译
        this.context=context;

    }

    public ArrayList<String> getData(){
        return mData;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {//该函数应该是在position位置绑定数据
//将资源绑定到相关组件


        String resName=mData.get(position);//取得其中一个资源名
//
//        CheckBox checkbox=((ViewHolder)holder).checkBox;
//        checkBoxs.add(checkbox);//保存好checkbox的引用，将来可以设置其显示与否
//        checkbox.setTag(resName);//设置好tag，再调用get可以获得一个resName



        String json=context.getExternalFilesDir("Res").getAbsolutePath()+"/"+resName+"/"+resName+".json";
        String jpg =context.getExternalFilesDir("Res").getAbsolutePath()+"/"+resName+"/"+resName+".jpg";
        String mp4 =context.getExternalFilesDir("Res").getAbsolutePath()+"/"+resName+"/"+resName+".mp4";
//        Toast.makeText(context, "取得的资源名为"+json, Toast.LENGTH_SHORT).show();
        try{

            Gson gson = new Gson();
            Reader reader = new FileReader(json);
            ResTextBean jsonBean = gson.fromJson(reader,ResTextBean.class);
//            Toast.makeText(context, ""+jsonBean.getTitle(), Toast.LENGTH_SHORT).show();

            String title=jsonBean.getTitle();
            String jText=jsonBean.getjText();
            SpannableStringBuilder jtextAfter=new SpannableStringBuilder("");
//            Toast.makeText(context, ""+jText, Toast.LENGTH_SHORT).show();
            try {
                jtextAfter.append(analyzeFromRes(jText));//服务器json的格式可能出错或者不规范，导致错误
            }catch (Exception e){
                jtextAfter.append("文本解析错误");
            }





            BitmapDrawable bitmapDrawable=new BitmapDrawable(jpg);//该方法可能产生过多内存

//将资源绑定到组件上
            DzVideo dzVideo=((ViewHolder)holder).dzVideo;
            dzVideo.setUp(mp4
                    , JZVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, title);
            dzVideo.thumbImageView.setImageDrawable(bitmapDrawable);


            ((ViewHolder)holder).textView.setText(jtextAfter);

        }catch (FileNotFoundException e){
            e.printStackTrace();
//                Toast.makeText(context, "有文件异常", Toast.LENGTH_SHORT).show();
        }




    }


    @Override
    public int getItemCount() {
        //返回子项的总生成数
//        return mData == null ? 0 : mData.size();
        return mData.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {//让RecyclerView知道子项长什么样oo
        //生成子项，并保持起来
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        ViewHolder viewHolder=new ViewHolder(v);

//以下四行为测试,不生效
        CheckBox checkbox=((ViewHolder)viewHolder).checkBox;
        checkBoxs.add(checkbox);//保存好checkbox的引用，将来可以设置其显示与否
        String resName=mData.get(checkBoxs.size()-1);//取得其中一个资源名
        checkbox.setTag(resName);//设置好tag，再调用get可以获得一个resName




//将创建的View注册点击事件


        return viewHolder;
    }





    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView;

        DzVideo dzVideo;
        public CheckBox checkBox;


        public ViewHolder(View itemView) {
            //初始化子项的组件
            super(itemView);
            textView = itemView.findViewById(R.id.recycler_view_item_text);
            dzVideo =  itemView.findViewById(R.id.dz_video);
            checkBox = itemView.findViewById(R.id.checkbox);



            jt=new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    if(checkBox.isChecked()){
                        deleteDatas.add((String)checkBox.getTag());//注意无顺序的情况
                        //Toast.makeText(context, "添加了删除项", Toast.LENGTH_SHORT).show();
                    }else {
                        deleteDatas.remove((String)checkBox.getTag());
                        //Toast.makeText(context, "减少了删除项", Toast.LENGTH_SHORT).show();
                    }
                }
            };

            checkBox.setOnClickListener(jt);
        }
    }
    //将普通字符串转换为带颜色的SpannableStringBuilder字符串的方法
    public SpannableStringBuilder analyzeFromRes(String string){

        String[] strings =string.split("\n",3);
        SpannableStringBuilder s0=new SpannableStringBuilder(strings[0]);
        SpannableStringBuilder s1=new SpannableStringBuilder(strings[1]);
        SpannableStringBuilder s2=new SpannableStringBuilder(strings[2]);
        int index=0;//索引修正数
        //处理第一个样式$1
        if(strings[0].contains("$1")){
            s0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue1)),strings[0].indexOf("$1")+2,strings[0].lastIndexOf("$1"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s0.delete(strings[0].indexOf("$1"),strings[0].indexOf("$1")+2);
            s0.delete(strings[0].lastIndexOf("$1")-2,strings[0].lastIndexOf("$1"));

            s1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue1)),strings[1].indexOf("$1")+2,strings[1].lastIndexOf("$1"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s1.delete(strings[1].indexOf("$1"),strings[1].indexOf("$1")+2);
            s1.delete(strings[1].lastIndexOf("$1")-2,strings[1].lastIndexOf("$1"));

            s2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue1)),strings[2].indexOf("$1")+2,strings[2].lastIndexOf("$1"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s2.delete(strings[2].indexOf("$1"),strings[2].indexOf("$1")+2);
            s2.delete(strings[2].lastIndexOf("$1")-2,strings[2].lastIndexOf("$1"));
            index+=4;
        }
        //处理第二个样式$2
        if(strings[0].contains("$2")){
            s0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_green1)),strings[0].indexOf("$2")+2-index,strings[0].lastIndexOf("$2")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s0.delete(strings[0].indexOf("$2")-index,strings[0].indexOf("$2")+2-index);
            s0.delete(strings[0].lastIndexOf("$2")-2-index,strings[0].lastIndexOf("$2")-index);

            s1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_green1)),strings[1].indexOf("$2")+2-index,strings[1].lastIndexOf("$2")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s1.delete(strings[1].indexOf("$2")-index,strings[1].indexOf("$2")+2-index);
            s1.delete(strings[1].lastIndexOf("$2")-2-index,strings[1].lastIndexOf("$2")-index);

            s2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_green1)),strings[2].indexOf("$2")+2-index,strings[2].lastIndexOf("$2")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s2.delete(strings[2].indexOf("$2")-index,strings[2].indexOf("$2")+2-index);
            s2.delete(strings[2].lastIndexOf("$2")-2-index,strings[2].lastIndexOf("$2")-index);

            index+=4;
        }
        //处理第三个样式$3
        if(strings[0].contains("$3")){
            s0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue2)),strings[0].indexOf("$3")+2-index,strings[0].lastIndexOf("$3")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s0.delete(strings[0].indexOf("$3")-index,strings[0].indexOf("$3")+2-index);
            s0.delete(strings[0].lastIndexOf("$3")-2-index,strings[0].lastIndexOf("$3")-index);

            s1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue2)),strings[1].indexOf("$3")+2-index,strings[1].lastIndexOf("$3")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s1.delete(strings[1].indexOf("$3")-index,strings[1].indexOf("$3")+2-index);
            s1.delete(strings[1].lastIndexOf("$3")-2-index,strings[1].lastIndexOf("$3")-index);

            s2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue2)),strings[2].indexOf("$3")+2-index,strings[2].lastIndexOf("$3")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s2.delete(strings[2].indexOf("$3")-index,strings[2].indexOf("$3")+2-index);
            s2.delete(strings[2].lastIndexOf("$3")-2-index,strings[2].lastIndexOf("$3")-index);

            index+=4;
        }
        //处理第四个样式$4
        if(strings[0].contains("$4")){
            s0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_green2)),strings[0].indexOf("$4")+2-index,strings[0].lastIndexOf("$4")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s0.delete(strings[0].indexOf("$4")-index,strings[0].indexOf("$4")+2-index);
            s0.delete(strings[0].lastIndexOf("$4")-2-index,strings[0].lastIndexOf("$4")-index);

            s1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_green2)),strings[1].indexOf("$4")+2-index,strings[1].lastIndexOf("$4")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s1.delete(strings[1].indexOf("$4")-index,strings[1].indexOf("$4")+2-index);
            s1.delete(strings[1].lastIndexOf("$4")-2-index,strings[1].lastIndexOf("$4")-index);

            s2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_green2)),strings[2].indexOf("$4")+2-index,strings[2].lastIndexOf("$4")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s2.delete(strings[2].indexOf("$4")-index,strings[2].indexOf("$4")+2-index);
            s2.delete(strings[2].lastIndexOf("$4")-2-index,strings[2].lastIndexOf("$4")-index);

            index+=4;
        }
        //处理第五个样式$5
        if(strings[0].contains("$5")){
            s0.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue3)),strings[0].indexOf("$5")+2-index,strings[0].lastIndexOf("$5")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s0.delete(strings[0].indexOf("$5")-index,strings[0].indexOf("$5")+2-index);
            s0.delete(strings[0].lastIndexOf("$5")-2-index,strings[0].lastIndexOf("$5")-index);

            s1.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue3)),strings[1].indexOf("$5")+2-index,strings[1].lastIndexOf("$5")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s1.delete(strings[1].indexOf("$5")-index,strings[1].indexOf("$5")+2-index);
            s1.delete(strings[1].lastIndexOf("$5")-2-index,strings[1].lastIndexOf("$5")-index);

            s2.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.tag_blue3)),strings[2].indexOf("$5")+2-index,strings[2].lastIndexOf("$5")-index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            s2.delete(strings[2].indexOf("$5")-index,strings[2].indexOf("$5")+2-index);
            s2.delete(strings[2].lastIndexOf("$5")-2-index,strings[2].lastIndexOf("$5")-index);

            index+=4;
        }
//        s2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.tag_blue1)),strings[2].indexOf("$1")+2,strings[2].lastIndexOf("$1"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        s2.delete(strings[2].indexOf("$1"),strings[2].indexOf("$1")+2);
//        s2.delete(strings[2].lastIndexOf("$1")-2,strings[2].lastIndexOf("$1"));
        s0.insert(0,"日文：");
        s1.insert(0,"发音：");
        s2.insert(0,"翻译：");
        return s0.append("\n").append(s1).append("\n").append(s2);

    }

    public ArrayList<String> getDeleteDatas(){
        return deleteDatas;
    }
    public ArrayList<CheckBox> getCheckBoxs(){
        return checkBoxs;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

    }
}
